//
//  main.m
//  05-HomeWork
//
//  Created by dllo on 16/7/8.
//  Copyright © 2016年 LST. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Person.h"
#import "ArrayClass.h"
#import "Pro.h"
#import "City.h"
#import "Zone.h"
#import "TempCity.h"
int main(int argc, const char * argv[]) {
    /*
     四、
     处理省市区文件中的area.txt
     将省市区进行分割处理, 省里包含市, 市里包含区
     1. 先用数组字典表示
     2. 将数组字典的处理方式转换成数组模型(Class)
     // 字典key 省 value***省
     // 字典key ***省 value(数组)市
     // 字典key***市 value (数组)区
     五、拓展
     上述作业做完, 处理Location1.txt
     要求同第四题。自己寻找规律
     province
     provincial city
     district
     */
    
    // #1
#if 0
    {
        NSArray *StringArray = @[@"0.0", @"x.x", @"@.@", @"123", @"456"];
        for (NSInteger i = StringArray.count - 1; i < StringArray.count; i--) {
            NSLog(@"%@", StringArray[i]);
        }
        NSMutableString *mutString = [[NSMutableString alloc] init];
        for (NSString *str in StringArray) {
            [mutString appendString:str];
        }
        NSLog(@"%@", mutString);
        
    }
#endif
    // #2 #3
#if 0
    {
        Person *person1 = [[Person alloc] initWithName:@"xiaohong1" age:18 sex:@"男"];
        Person *person2 = [[Person alloc] initWithName:@"xiaohong2" age:19 sex:@"男"];
        Person *person3 = [[Person alloc] initWithName:@"xiaohong3" age:20 sex:@"女"];
        Person *person4 = [[Person alloc] initWithName:@"xiaohong4" age:21 sex:@"男"];
        Person *person5 = [[Person alloc] initWithName:@"xiaohong5" age:22 sex:@"女"];
        
        NSInteger max = 0;
        int item = 0;
        NSArray *personArr = @[person1, person2, person3, person4, person5];
        for (int i = 0; i < personArr.count; i++) {
            if ([personArr[i] age] >= max) {
                max = [personArr[i] age];
            }
            max = [personArr[i] age];
            item = i;
        }
        // NSLog(@"%@", personArr[item]);
        for (Person *person in personArr) {
            if (person.age > 20) {
                person.name = [person.name stringByAppendingString:@"-brother"];
            }
        }
        // NSLog(@"%@", personArr);
        // #3.2
        NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:NO];
        personArr = [personArr sortedArrayUsingDescriptors:@[sortDescriptor]];
        NSLog(@"%@", personArr);
        // #3.1
        personArr = [personArr sortedArrayUsingSelector:@selector(compareWithAge:)];
        NSLog(@"%@", personArr);
        personArr = [personArr sortedArrayUsingSelector:@selector(compareWithName:)];
        NSLog(@"%@", personArr);
        personArr = [personArr sortedArrayUsingSelector:@selector(compareWithSex:)];
        NSLog(@"%@", personArr);
        
    }
#endif
    // #4.1 重点是通过lastObject获取
#if 0
    {
        NSString * dataString = [NSString stringWithContentsOfFile:@"/Users/dllo/DLS160710/Objective-C/HomeWork/05-HomeWork/05-HomeWork/area.txt" encoding:NSUTF8StringEncoding error:nil];
        NSArray *allArray = [dataString componentsSeparatedByString:@"\n"];
        // 1.省数组 - 省字典 - 市数组 - 市字典 - 区数组 - 区字典
        NSMutableArray *allProvinceArray = [NSMutableArray array];
        for (NSString *area in allArray) {
            // 取到所有的省(没有空格开头)
            if (![area hasPrefix:@"  "]) {
                // 创建了省字典(包含两个字段 name array)
                NSMutableDictionary *proDic = [NSMutableDictionary dictionary];
                // 添加proName字段
                [proDic setObject:area forKey:@"proName"];
                // 创建了省中所有市的数组
                NSMutableArray *cityArray = [NSMutableArray array];
                // 添加数组字段
                [proDic setObject:cityArray forKey:@"cityArray"];
                // 将创建好的省字典放到省数组中
                [allProvinceArray addObject:area];
                // 取到所有的市(两个空格开头, 但不是4个空格开头)
            } else if ([area hasPrefix:@"  "] && ![area hasPrefix:@"    "]) {
                // 难点: 通过lastObject获取到对应的省字典
                NSMutableDictionary *proDic = [allProvinceArray lastObject];
                // 拿到省字典中对应的市数组
                NSMutableArray *cityArray = [proDic objectForKey:@"cityArray"];
                 // 创建 市字典 (name 和 区array)
                NSMutableDictionary *cityDic = [NSMutableDictionary dictionary];
                [cityDic setObject:area forKey:@"cityName"];
                NSMutableArray *zoneArray = [NSMutableArray array];
                [cityDic setObject:zoneArray forKey:@"zoneArray"];
                // 向市数组中添加市字典
                [cityArray addObject:cityDic];
                // 取到所有的区/县(4个空格开头)
            } else {
                // 通过省数组获取到当前所在省
                NSMutableDictionary *proDic = [allProvinceArray lastObject];
                // 通过省字典获取到市数组
                NSMutableArray *cityArray = [proDic objectForKey:@"cityArray"];
                // 通过市数组获取到对应的市字典
                NSMutableDictionary *city = [cityArray lastObject];
                // 通过市字典取到对应的区/县的数组
                NSMutableArray *zoneArray = [city objectForKey:@"zoneArray"];
                // 最终将区/县添加到数组中
                [zoneArray addObject:area];
            }
        }
        NSLog(@"%@", allProvinceArray);
        // 2.省数组 - province类
        NSMutableArray *allproArray = [NSMutableArray array];
        for (NSString *area in allArray) {
            if (![area hasPrefix:@" "]) {
                Pro *pro = [Pro zoneWithName:area];
                [allproArray addObject:pro];
            } else if ([area hasPrefix:@"  "] && ![area hasPrefix:@"    "]) {
                Pro *pro = [allproArray lastObject];
                City *city = [City zoneWithName:area];
                [pro.Array addObject:city];
            } else {
                City *city = [[[allproArray lastObject] Array]lastObject];
                Zone *zone = [Zone zoneWithName:area];
                [city.Array addObject:zone];
            }
        }
        
        NSLog(@"%@", allArray);
        
        
        
    }
#endif
    
#if 0
    
    
        NSString *path = @"/Users/dllo/DLS160710/Objective-C/HomeWork/05-HomeWork/05-HomeWork/area.txt";
        // 拿到数据字符串
        NSString *areaString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
        // 根据回车, 分割字符串
        NSArray *strArr = [areaString componentsSeparatedByString:@"\n"];
        NSMutableArray *mutArray = [NSMutableArray array];
        for (NSString *temp in strArr) {
            // 取到所有的省
            if (![temp hasPrefix:@" "]) {
                NSMutableDictionary *mutDic = [NSMutableDictionary dictionary];
                [mutDic setObject:temp forKey:@"proName"];
                NSMutableArray *cityArr = [NSMutableArray array];
                [mutDic setObject:cityArr forKey:@"cities"];
                [mutArray addObject:mutDic];
                // 取到所有的市
            } else if ([temp hasPrefix:@"  "] && ![temp hasPrefix:@"    "]) {
                NSMutableDictionary *cityDic = [NSMutableDictionary dictionary];
                [cityDic setObject:temp forKey:@"cityName"];
                NSMutableArray *areaArr = [NSMutableArray array];
                [cityDic setObject:areaArr forKey:@"areas"];
                NSMutableDictionary *proDic = [mutArray lastObject];
                [[proDic objectForKey:@"cities"]addObject:cityDic];
                // 取到所有的区/县
            } else {
                NSMutableDictionary *areaDic = [NSMutableDictionary dictionary];
                [areaDic setObject:temp forKey:@"areaName"];
                [[[[[mutArray lastObject] objectForKey:@"cities"] lastObject] objectForKey:@"areas"] addObject:areaDic];
            }
        }
        NSLog(@"%@", mutArray);
        for (NSMutableDictionary *proDic in mutArray) {
            ArrayClass *arrClass = [[ArrayClass alloc] init];
            arrClass.province = [proDic objectForKey:@"proName"];
            arrClass.cityArr = [proDic objectForKey:@"cities"];
            NSLog(@"%@-%@", arrClass.province, arrClass.cityArr);
            for (NSMutableDictionary *city in arrClass.cityArr) {
                arrClass.city = [city objectForKey:@"cityName"];
                arrClass.disArr = [city objectForKey:@"areas"];
                NSLog(@"%@-%@", arrClass.city, arrClass.disArr);
                for (NSMutableDictionary *area in arrClass.disArr) {
                    arrClass.district = [area objectForKey:@"areaName"];
                    arrClass.disArr = [area objectForKey:@"areas"];
                    // NSLog(@"%@", arrClass.district);
                }
            }
        }
        
    }
#endif
    // #5 重点是排序
#if 1
    {
        NSString *locationPath = @"/Users/dllo/DLS160710/Objective-C/HomeWork/05-HomeWork/05-HomeWork/Location1.txt";
        NSString *soure = [NSString stringWithContentsOfFile:locationPath encoding:NSUTF8StringEncoding error:nil];
        // 根据 , 分割字符串, 得到所有城市的数组
        NSArray *allCityArray = [soure componentsSeparatedByString:@","];
        NSMutableArray *allCityDicArray = [NSMutableArray array];
        // 根据每个城市的|处理字符串, 重新生成字典的数组
        for (NSString *city in allCityArray) {
            // 根据|切割字符串
            NSArray *array = [city componentsSeparatedByString:@"|"];
            // 根据生成的数组, 生成新的城市字典
            NSMutableDictionary *cityDic = [NSMutableDictionary dictionaryWithObjectsAndKeys:[array firstObject], @"cityName", [array lastObject], @"cityNumber", nil];
            // 添加到新的数组中
            [allCityDicArray addObject:cityDic];
        }
        // 排序 (重要)
        NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"cityNumber" ascending:YES];
        [allCityDicArray sortUsingDescriptors:@[sort]];
        NSMutableArray *finalArray = [NSMutableArray array];
        for (NSMutableDictionary *cityDic in allCityDicArray) {
            NSString *cityNumber = cityDic[@"cityNumber"];
            if ([cityNumber hasSuffix:@"0000"]) {
                NSMutableDictionary *proDic = [NSMutableDictionary dictionaryWithDictionary:cityDic];
                [proDic setObject:[NSMutableArray array] forKey:@"cityArray"];
                [finalArray addObject:proDic];
            } else if ([cityNumber hasSuffix:@"00"]) {
                NSMutableDictionary *proDic = [finalArray lastObject];
                NSMutableArray *cityArray = [proDic objectForKey:@"cityArray"];
                NSMutableDictionary *dicCity = [NSMutableDictionary dictionaryWithDictionary:cityDic];
                [dicCity setObject:[NSMutableArray array] forKey:@"zoneArray"];
                [cityArray addObject:dicCity];
            } else {
                // 注意: 写代码尽量分开写
                NSMutableDictionary *proDic = [finalArray lastObject];
                NSMutableArray *cityArray = [proDic objectForKey:@"cityArray"];
                NSMutableDictionary *dicCity = [cityArray lastObject];
                NSMutableArray *zoneArray = [dicCity objectForKey:@"zoneArray"];
                NSMutableDictionary *zoneDic = [NSMutableDictionary dictionaryWithDictionary:cityDic];
                [zoneArray addObject:zoneDic];
            }
        }
        //NSLog(@"%@", finalArray);
        
        NSMutableArray *allTempCity = [NSMutableArray array];
        for (NSString *cityString in allCityArray) {
            // 根据|切割字符串
            NSArray *city = [cityString componentsSeparatedByString:@"|"];
            TempCity *tempCity = [[TempCity alloc] initWithName:[city firstObject] number:[city lastObject]];
            [allTempCity addObject:tempCity];
        }
        // 对类数组排序
        NSSortDescriptor *sortTempCity = [[NSSortDescriptor alloc] initWithKey:@"cityNumber" ascending:YES];
        [allTempCity sortUsingDescriptors:@[sortTempCity]];
       // NSLog(@"%@",allTempCity);
        NSMutableArray *finalClassArray = [NSMutableArray array];
        for (TempCity *tempCity in allTempCity) {
            if ([tempCity.cityNumber hasSuffix:@"0000"]) {
                Pro *pro = [Pro zoneWithTempCity:(tempCity)];
                [finalClassArray addObject:pro];
            } else if ([tempCity.cityNumber hasSuffix:@"00"] && ![tempCity.cityNumber hasSuffix:@"0000"]) {
                Pro *pro = [finalClassArray lastObject];
                City *city = [City zoneWithTempCity:tempCity];
                [pro.Array addObject:city];
            } else {
                Pro *pro = [finalClassArray lastObject];
                City *city = [pro.Array lastObject];
                Zone *zone = [Zone zoneWithTempCity:tempCity];
                [city.Array addObject:zone];
            }
        }
        NSLog(@"%@", finalClassArray);
    }
#endif
    
    return 0;
}


